<?php


namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Permission extends Controller
{
    protected $beforeActionList = [
        'init' => ['except'=>'save,change'],
    ];

    public function init()
    {
        $request= Request::instance();
        $a=$request->controller();
        $b=$request->action();

        $pid=Db::table('hb_permission')
            ->field('id')
            ->where('controller',$a)
            ->where('method',$b)
            ->find();
//dump($pid);
        $a=Session::get('pid');
//        dump($a);
        if (!in_array($pid['id'],$a))
        {
            return $this->error('没有此权限,沙雕!!');
        }

    }


    public function index()
    {

        $result= Db::name('permission')->select();

        return view('admin@permission/index',[
            'data'=>$result,
            'title'=>'权限列表'

        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //        return '添加表单';
        return view('permission/create', [
            'title'=>'填个权限!'
        ]);
    }


    /**
     * 保存新建的资源
     *
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $p=$request->post();
        $result = Db::name('permission')
            ->data($p)
            ->insert();
        if ($result>0) {
            return $this->success('添加成功', url('admin/permission/index'));
        } else {
            return $this->error('添加失败,请重试!!');
        }
    }
}